package Sequence;

public class Test0309 {
}

class Solution0309_1 {
    public int maxProfit(int[] prices) {
        int length = prices.length;
        if (length < 2) return 0;

        int[] dp = new int[4];
        dp[0] = -prices[0];
        int[] dp1 = new int[4];

        for (int i = 1; i < length; i++) {
            int price = prices[i];
            dp1[0]= Math.max(dp[0],dp[2]-price);
            dp1[0]= Math.max(dp1[0],dp[3]-price);

            dp1[1]=dp[0]+price;

            dp1[2]=dp[1];

            dp1[3]=Math.max(dp[3],dp[2]);

            int[] temp = dp;
            dp = dp1;
            dp1 = temp;
        }
        int max1 = Math.max(dp[3], dp[1]);
        int max2 = Math.max(dp[2], dp[0]);
        return Math.max(max1, max2);
    }
}
